﻿using System;
using System.Data;
using System.Linq;

namespace ExchangeFilesPharmWinService
{
    class LogClass
    {
        public void LogIns(int logHeaderID, string fileName, long fileSize, string checkSum, string exceptionMessage)
        {
            var l = new LogDBEntities();
            try
            {
                var logs = new Logs();
                logs.LogID = LogsGetMaxID() + 1;
                logs.LogTime = DateTime.Now;
                if (fileName != "") logs.FileName = fileName;
                if (fileSize != 0) logs.FileSize = fileSize;
                if (checkSum != "") logs.CheckSum = checkSum;
                logs.LogHeaderID = logHeaderID;
                if (exceptionMessage != "")
                {
                    if (exceptionMessage.Length > 512) logs.ExceptionMessage = exceptionMessage.Substring(0, 512);
                    else logs.ExceptionMessage = exceptionMessage;
                }
                l.AddObject("Logs", logs);
                l.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("LogIns(int logHeaderID, string fileName, long fileSize, string checkSum, string exceptionMessage)\n" + ex.Message);
            }
            finally
            {
                if (l.Connection.State != ConnectionState.Closed)
                    l.Connection.Close();
                l = null;
            }

        }

        public void LogIns(int logHeaderID, string fileName, long fileSize, string checkSum)
        {
            var l = new LogDBEntities();
            try
            {
                var logs = new Logs();
                logs.LogID = LogsGetMaxID() + 1;
                logs.LogTime = DateTime.Now;
                if (fileName != "") logs.FileName = fileName;
                if (fileSize != 0) logs.FileSize = fileSize;
                if (checkSum != "") logs.CheckSum = checkSum;
                logs.LogHeaderID = logHeaderID;
                l.AddObject("Logs", logs);
                l.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("LogIns(int logHeaderID, string fileName, long fileSize, string checkSum)\n" + ex.Message);
            }
            finally
            {
                if (l.Connection.State != ConnectionState.Closed)
                    l.Connection.Close();
                l = null;
            }
        }

        public void LogIns(int logHeaderID)
        {
            var l = new LogDBEntities();
            try
            {
                var logs = new Logs();
                logs.LogID = LogsGetMaxID() + 1;
                logs.LogTime = DateTime.Now;
                logs.LogHeaderID = logHeaderID;
                l.AddObject("Logs",logs);
                l.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("LogIns(int logHeaderID)\n" + ex.Message);
            }
            finally
            {
                if (l.Connection.State != ConnectionState.Closed)
                    l.Connection.Close();
                l = null;
            }
        }

        public void LogIns(int logHeaderID, string exceptionMessage)
        {
            var l = new LogDBEntities();
            try
            {
                var logs = new Logs();
                logs.LogID = LogsGetMaxID() + 1;
                logs.LogTime = DateTime.Now;
                logs.LogHeaderID = logHeaderID;
                if (exceptionMessage != "")
                {
                    if (exceptionMessage.Length > 512) logs.ExceptionMessage = exceptionMessage.Substring(0, 512);
                    else logs.ExceptionMessage = exceptionMessage;
                }
                l.AddObject("Logs", logs);
                l.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("LogIns(int logHeaderID, string exceptionMessage)\n" + ex.Message);
            }
            finally
            {
                if (l.Connection.State != ConnectionState.Closed)
                    l.Connection.Close();
                l = null;
            }
        }

        private long LogsGetMaxID()
        {
            var res = from t in new LogDBEntities().Logs
                      select t.LogID;
            if (res.Count() == 0) return 0;
            else return res.Max(); 
        }
    }
}
